package com.cn.unarmed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.cn.unarmed.adapter.CheckListLinearAdapter;
import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.bean.DataHolder;
import com.cn.unarmed.commonutils.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 线性布局设定选中listView的Item
 */
public class CheckableLinearLayoutActivity extends BaseActivity implements View.OnClickListener{

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_list_linear);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private ListView list_check;
    Button check_all,check_none,check_size,grid_item;
    CheckListLinearAdapter adapter;
    List<DataHolder> list;
    List<Boolean> list_set;

    public void initView(){
        list_check = (ListView)this.findViewById(R.id.list_check);
        check_all = (Button)this.findViewById(R.id.check_all);
        check_none = (Button)this.findViewById(R.id.check_none);
        check_size = (Button)this.findViewById(R.id.check_size);
        grid_item = (Button)this.findViewById(R.id.grid_item);

        check_all.setOnClickListener(this);
        check_none.setOnClickListener(this);
        check_size.setOnClickListener(this);
        grid_item.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.check_all:
                for (int i = 0; i < list_set.size(); i++) {
                    list_set.set(i,true);
                }
                adapter = new CheckListLinearAdapter(this,list,list_set);
                list_check.setAdapter(adapter);
                AppUtils.setListViewHeightBasedOnChildren(list_check);
                break;
            case R.id.check_none:
                for (int i = 0; i < list_set.size(); i++) {
                    list_set.set(i,false);
                }
                adapter = new CheckListLinearAdapter(this,list,list_set);
                list_check.setAdapter(adapter);
                AppUtils.setListViewHeightBasedOnChildren(list_check);
                break;
            case R.id.check_size:
                int have = 0;
                if (list_set != null){
                    for (int i = 0; i < list_set.size(); i++) {
                        if (list_set.get(i)) {
                            have++;
                        }
                    }
                    Log.i("Tag","选中的条目有" + have + "这么多条");
                }
                break;
            case R.id.grid_item:
                startActivity(new Intent(this,DifferentGridItemActivity.class));
                break;
        }
    }

    public void initData(){
        list = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            list.add(new DataHolder("harvic的blog------" + i, "harvic"));
        }
        list_set = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list_set.add(i,false);
        }
        adapter = new CheckListLinearAdapter(this,list,list_set);
        list_check.setAdapter(adapter);
        AppUtils.setListViewHeightBasedOnChildren(list_check);
    }
}
